package com.lkx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkx.common.R;
import com.lkx.pojo.Category;
import com.lkx.service.CategoryService;
import com.lkx.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: com.lkx.controller.CategoryController
 * @author: likaixin
 * @create: 2022年11月22日 15:41
 * @desc: 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService service;
    //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        service.save(category);
        return R.success("新增分类成功！");
    }

    //分页展示
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        //添加排序条件
        lqw.orderByAsc(Category::getSort);
        service.page(categoryPage,lqw);
        return R.success(categoryPage);
    }

    //删除分类
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类，id为:{}",ids);
        service.remove(ids);
        return R.success("分类信息删除成功！");
    }

    //根据id来修改分类信息
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        service.updateById(category);
        return R.success("修改分类信息成功！");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = service.list(lqw);

        return R.success(list);
    }
}
